#ifndef HPLCTIMEPROGRAM_H
#define HPLCTIMEPROGRAM_H

#include <QObject>
#include <QTimer>
#include "hplctimeprogramunit.h"
#include "modulehplcpump.h"
#include "modulehplcdetector.h"
#include "hplcinjectprogram.h"

class HplcTimeProgram : public QObject
{
    Q_OBJECT
public:
    HplcTimeProgram(QObject *parent = nullptr);

    void setTickPeriod(double period_s);

    void setProgram(const QList<TimeProgramUnit> &list);
    QList<TimeProgramUnit> getProgram() const;

    void setLoopTimes(int times);
    int getLoopTimes() const;

    void setUsedTimeProgram(bool flag);
    bool getUsedTimeProgram() const;

    void setSampleTime(double second);
    double getSampleTime() const;

    void setTotalRunTime(double second);
    double getTotalRunTime() const;

    void setInjectFlow(double flow);
    double getInjectFlow();

    void setInjectTime(double second);
    double getInjectTime();

    bool isRunning();
    bool isPause();

    // 命令接口
    void start();
    void startFirstRow();
    void startBasicParam();
    void pause();
    void restore();
    void stop();
    void startStopAllPump(bool isStart);

    void startInjectProgram();

signals:
//    void updateState(int id, const QVariant &var);

    void injectProgramFinished();
    void rowChanged(int row);
    void loopTimesChanged(int loopTimes);
    void timeProgramFinished();
    void sampleTimeout();
    void totalRunTimeout();
    void statusChanged(PumpUnit status);

public slots:
    void onTick();

private:
    bool isValid();
    void next();
    void executeRow();
    void execute();

private:
    ModuleHplcPump              *m_pump[HPLC_TIME_PROGRAM_PUMP_COUNT];
    ModuleHplcDetector          *m_detector;

    double                      m_tickPeriod = 0.5; // s
    bool                        m_isStart = false;
    bool                        m_isPause = true;
    double                      m_tick = 0; // s
    double                      m_sampleTime = 60 * 60; // s
    double                      m_totalRunTime = 400 * 60; // s

    QList<TimeProgramUnit>      m_program;
    int                         m_currentIndex = 0;
    int                         m_loopTimes = 0;
    bool                        m_isUsedTimeProgram = true;
    bool                        m_isSampleTimeout = false;
    bool                        m_isTimeProgramFinish = false;

    TimeProgramUnit             *m_current;
    TimeProgramUnit             *m_next;
    TimeProgramUnit             m_basicParam;

    DetectorUnit                m_prevDetectorUnit;
    HplcTimeProgramUnit         *m_unit;

    // 注样程序
    HplcInjectProgram           *m_injectProgram;
    bool                        m_isStartInjectProgram;
};

#endif // HPLCTIMEPROGRAM_H
